<template>
  <el-card class="sys-tagtable">
    <tag-content-header add search refresh @toolFun="toolFun" :settingData="columns">
      <template #title>
        <span>{{ tableType ? '明细表' : '标签表' }}列表</span>
      </template>
    </tag-content-header>
    <tag-table
      ref="table"
      @refresh="getList"
      :data="dataList"
      :loading="loading"
      :columns="columns"
      :params="params"
      :total="totalSize"
      @rowClick="rowClick"
    >
      <template #operation="{ scope }">
        <div class="operation-button-box">
          <!-- <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
            <icon-svg name="iconEdit" class="svg-edit" @click.native="handleEdit('edit', scope.row)"></icon-svg>
          </el-tooltip> -->
          <el-tooltip effect="dark" content="查看字段名列表" placement="top" :enterable="false">
            <icon-svg
              name="iconPreview"
              class="svg-edit"
              v-if="tableType"
              @click.native.stop="handlePreview(scope.row)"
            ></icon-svg>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
            <icon-svg name="iconDelete" class="svg-delete" @click.native.stop="handleDelete(scope.row)"></icon-svg>
          </el-tooltip>
        </div>
      </template>
    </tag-table>
    <tagtable-drawer ref="tagtableDrawer" @refresh="getList"></tagtable-drawer>
    <tagtable-field-drawer ref="tagtableFieldDrawer"></tagtable-field-drawer>
  </el-card>
</template>

<script>
import tagtableDrawer from './tagtable-edit-item'
import tagtableFieldDrawer from './tagtable-field-drawer'
export default {
  props: {
    tableType: {
      type: Number,
      default: 0
    }
  },
  components: {
    tagtableDrawer,
    tagtableFieldDrawer
  },
  data() {
    return {
      loading: false,
      dataList: [],
      columns: [
        {
          show: true,
          label: '数据表名称',
          prop: 'tableNm',
          search: true,
          searchKey: ''
        },
        {
          show: true,
          label: '归属平台',
          prop: 'plfmNm',
          search: true,
          searchKey: '',
          align: 'center'
        },
        {
          show: true,
          label: '归属数据库',
          prop: 'dbNm',
          search: true,
          searchKey: '',
          align: 'center'
        },
        {
          show: true,
          label: '关联字段',
          prop: 'relaField',
          search: true,
          searchKey: '',
          align: 'center'
        },
        {
          show: true,
          label: '创建时间',
          prop: 'createTm',
          align: 'center'
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 200
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  // created() {
  //   this.getList()
  // },
  methods: {
    rowClick(row) {
      if (row.tableNm) this.handleEdit('edit', Object.assign({}, row, { tableType: this.tableType }))
    },
    getList() {
      this.loading = true
      this.params.queryParams.tableType = this.tableType
      this.$ajax({
        url: this.$ajax.setUrl('mta/mtaData/pageList'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.dataList = data.data.records
          this.totalSize = data.data.totalSize
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    handleEdit(type = 'add', scope = {}) {
      this.$refs.tagtableDrawer.showDrawer(type, scope)
    },
    toolFun(operation) {
      switch (operation) {
        case 'add':
          this.handleEdit('add', { tableType: this.tableType })
          break
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    handleDelete(scope) {
      if (!scope.tableNm) return
      this.$confirm('确认删除【' + scope.tableNm + '】？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl(`mta/mtaData/delete/${scope.plfmNm}/${scope.dbNm}/${scope.tableNm}`),
            method: 'delete',
            params: this.$ajax.setParams()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.getList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {
          return
        })
    },
    handlePreview(scope) {
      this.$refs.tagtableFieldDrawer.showDrawer(scope)
    }
  }
}
</script>

<style lang="scss">
.sys-tagtable {
  .tag-table .el-table__row {
    cursor: pointer;
  }
}
</style>
